package com.dg.acb.utils.configdata;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by fzh on 17-9-6.
 */

public class XXUserInfoData extends JSONObject implements Parcelable {
    public final static byte LOGIN_TRUE = 1;
    public final static byte LOGIN_FALSE = 0;
    public boolean mIsLogin = false;
    public int mStatus = 0;
    public long mId = 0;
    public String mHeadUrl = "";
    public String mToken = "";
    public String mUser = "";

    public final static String IS_LOGIN_KEY = "isLogin";
    public final static String USER_NAME_KEY = "userName";
    public final static String HEAD_PHOTO_KEY = "headPhoto";
    public final static String TOKEN_KEY = "token";
    public XXUserInfoData(boolean isLogin, String user, String token,String headUrl){
        mIsLogin = isLogin;
        mHeadUrl = headUrl;
        mToken = token;
        mUser = user;
        putAllData();
    }
    public XXUserInfoData(){

    }

    private void putAllData(){
        try {
            this.put(IS_LOGIN_KEY,mIsLogin);
            this.put(USER_NAME_KEY,mUser);
            this.put(HEAD_PHOTO_KEY,mHeadUrl);
            this.put(TOKEN_KEY,mToken);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public XXUserInfoData(String jsonStr) throws JSONException {
        super(jsonStr);
        mIsLogin = getBoolean(IS_LOGIN_KEY);
        mHeadUrl = getString(HEAD_PHOTO_KEY);
        mToken = getString(TOKEN_KEY);
        mUser = getString(USER_NAME_KEY );
    }

    protected XXUserInfoData(Parcel in) {
        byte isLogin = in.readByte();
        if(isLogin == LOGIN_TRUE){
            mIsLogin = true;
        }else{
            mIsLogin = false;
        }
        mStatus = in.readInt();
        mId = in.readLong();
        mHeadUrl = in.readString();
        mUser = in.readString();
        mToken = in.readString();
        putAllData();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if(mIsLogin){
            dest.writeByte(LOGIN_TRUE);
        }else{
            dest.writeByte(LOGIN_FALSE);
        }
        dest.writeInt(mStatus);
        dest.writeLong(mId);
        dest.writeString(mHeadUrl);
        dest.writeString(mUser);
        dest.writeString(mToken);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<XXUserInfoData> CREATOR = new Creator<XXUserInfoData>() {
        @Override
        public XXUserInfoData createFromParcel(Parcel in) {
            return new XXUserInfoData(in);
        }

        @Override
        public XXUserInfoData[] newArray(int size) {
            return new XXUserInfoData[size];
        }
    };

    /*
    public String toString(){
        StringBuilder builder = new StringBuilder();
        String split = "  ";
        builder.append("isLogin:");
        builder.append(mIsLogin);
        builder.append(split);
        builder.append("status:");
        builder.append(mStatus);
        builder.append(split);
        builder.append("id");
        builder.append(mId);
        builder.append(split);
        builder.append("headUrl:");
        builder.append(mHeadUrl);
        builder.append(split);
        builder.append("user:");
        builder.append(mUser);
        builder.append(split);
        builder.append("token");
        builder.append(mToken);
        return builder.toString();
    }*/
}
